from urllib.parse import quote_plus

import pandas as pd
import requests
from setuptools.unicode_utils import try_encode
from sqlalchemy import create_engine


def mysql_config():
    user = 'root'
    password = quote_plus('Jison0820')
    # passwd ='merchantsasd123!@%&'
    host = 'rm-bp1be6s3581xp13rfco.mysql.rds.aliyuncs.com'
    # port1 ='3306'
    dbname2 = 'data-warehouse'
    engine2 = create_engine(f"mysql+pymysql://{user}:{password}@{host}:3306/{dbname2}?charset=utf8mb4")
    return engine2

def requests_danmu(item_id,cookies):

    headers = {"cookie":cookies}
    request_data = {
        "item_id":item_id,
        "metrics":["danmaku_count"]
    }
    url = 'https://creator.douyin.com/janus/douyin/creator/data/item_analysis/metrics_trend?aid=2906&app_name=aweme_creator_platform&device_platform=web&referer=https:%2F%2Fcreator.douyin.com%2Fcreator-micro%2Fcontent%2Fmanage&user_agent=Mozilla%2F5.0+(Macintosh%3B+Intel+Mac+OS+X+10_15_7)+AppleWebKit%2F537.36+(KHTML,+like+Gecko)+Chrome%2F129.0.0.0+Safari%2F537.36&cookie_enabled=true&screen_width=1512&screen_height=982&browser_language=zh-CN&browser_platform=MacIntel&browser_name=Mozilla&browser_version=5.0+(Macintosh%3B+Intel+Mac+OS+X+10_15_7)+AppleWebKit%2F537.36+(KHTML,+like+Gecko)+Chrome%2F129.0.0.0+Safari%2F537.36&browser_online=true&timezone_name=Asia%2FShanghai&time_unit=1&trend_type=2&metrics_group=[0]'
    response = requests.get(url, headers=headers, params=request_data)
    print(response.json(),"**********************************************************************")
    if response.json()["trend_map"] == {}:
        danmu_frame = pd.DataFrame()
        danmu_frame["aweme_id"] = item_id
        danmu_frame["date_time"] = ''
        danmu_frame["danmaku_count"] = '0'

    else:
        # print("不为空")
        trend_data = response.json()["trend_map"]["danmaku_count"]['0']
        danmu_list = []
        for i in trend_data:
            danmu_list.append(i)
        danmu_frame = pd.DataFrame(danmu_list)
        danmu_frame["aweme_id"] = item_id
        danmu_frame.rename(columns={"value":"danmaku_count"},inplace=True)
        # danmu_frame
        print("在这里")
        return danmu_frame

# print(requests_danmu())
def job1_danmu(douyin_account_id,cookies):
    info_data = pd.read_sql("select * from data_warehouse_douyin_aweme_info_data where douyin_code='{}'".format(douyin_account_id),mysql_config())
    data_list = [info_data.loc[i].to_dict() for i in info_data.index.values]
    danmu_frame =  pd.DataFrame()
    for i in data_list:
        cc = requests_danmu(i["aweme_id"],cookies)

        danmu_frame = pd.concat([danmu_frame,cc],axis=0)
    danmu_frame = danmu_frame.drop(["aweme_id"],axis=1)
    danmu_frame = danmu_frame.drop(["date_time"],axis=1)
    return danmu_frame

# print(job1_danmu('1847014100','passport_csrf_token=2a8a3b432fa2f96cd4bcd932aa27692d; passport_csrf_token_default=2a8a3b432fa2f96cd4bcd932aa27692d; bd_ticket_guard_client_web_domain=2; s_v_web_id=verify_m2vurr8m_kBqePjIr_Fawj_4VzJ_8Adl_DWbSzl7TDgWn; passport_mfa_token=CjVO9dYD903oNtWZoBC0IBIWYW0V31VkU6m8yT4I%2Bi8zAiBxasQ7XIFWc6mG7mkwmn79at3WyRpKCjxW9NwnQi0bX2eAZ74xJlcJ9h0L%2B9bbmsfUIh1kwDBzsGrbAGXfGGl7e88P9U0%2FBtaCyq6lQSuv2k0i99gQuJDgDRj2sdFsIAIiAQMou99v; d_ticket=8f48eaca7d47dabe980aa4a0cf6835bddc00a; passport_assist_user=Cj1N0oaeM1IDg62Evqk1ziN-CU2fsFFC8e3MXboYecpHnZNZrqhkfaLB12t2KSab0CuGUc_FVZ92MpjNsE5WGkoKPCOtjmqtbA8vMEZKR4WC_n2OqWNms7x1eWMfIoJzMGuZIKEv_NH2td2yF6Lv-CZUkE4ywe2CIOnKPELlphC0kOANGImv1lQgASIBA_SL1sc%3D; n_mh=5N4y9MsfId8_tQB-HMaaxgc0XO7ji9tQlkHGqalhvZA; sso_uid_tt=10280c67f3b27781454eb3881fa5bfd0; sso_uid_tt_ss=10280c67f3b27781454eb3881fa5bfd0; toutiao_sso_user=109366d68799b28ac5eb3d9b90bfc2cf; toutiao_sso_user_ss=109366d68799b28ac5eb3d9b90bfc2cf; sid_ucp_sso_v1=1.0.0-KDJjZGE5OTg0NDgyYzJmYTAwNGQzMGFkMTBlYjZkYjE2MjJjZTRhNWEKHwjDvZj8lAMQ9MyIuQYY2hYgDDCU-MPgBTgGQPQHSAYaAmxxIiAxMDkzNjZkNjg3OTliMjhhYzVlYjNkOWI5MGJmYzJjZg; ssid_ucp_sso_v1=1.0.0-KDJjZGE5OTg0NDgyYzJmYTAwNGQzMGFkMTBlYjZkYjE2MjJjZTRhNWEKHwjDvZj8lAMQ9MyIuQYY2hYgDDCU-MPgBTgGQPQHSAYaAmxxIiAxMDkzNjZkNjg3OTliMjhhYzVlYjNkOWI5MGJmYzJjZg; odin_tt=6279bc252181dba6e228f85c6402e529a0013740ca3d861012f268c841094c749ce5b37899f2d238a64284166feec93d89972ecca64f5d521cc2fc82054c84ee; passport_auth_status=71a5183d47004193c3b7eb88718fa986%2C; passport_auth_status_ss=71a5183d47004193c3b7eb88718fa986%2C; uid_tt=86b87a60029a29805cd019944d702014; uid_tt_ss=86b87a60029a29805cd019944d702014; sid_tt=f1010cc96ab20a1aa64f2c2267d68d68; sessionid=f1010cc96ab20a1aa64f2c2267d68d68; sessionid_ss=f1010cc96ab20a1aa64f2c2267d68d68; is_staff_user=false; oc_login_type=LOGIN_PERSON; _bd_ticket_crypt_doamin=2; _bd_ticket_crypt_cookie=8cf513e5f08de3032e533d6fb08418d1; __security_server_data_status=1; sid_guard=f1010cc96ab20a1aa64f2c2267d68d68%7C1730291323%7C5183996%7CSun%2C+29-Dec-2024+12%3A28%3A39+GMT; sid_ucp_v1=1.0.0-KDQ1YTI5MDU4MzAwZGRmNGQxNTBiOWUyYTc4YjMxMjBhZGExMTA4MjYKGQjDvZj8lAMQ-8yIuQYY2hYgDDgGQPQHSAQaAmxmIiBmMTAxMGNjOTZhYjIwYTFhYTY0ZjJjMjI2N2Q2OGQ2OA; ssid_ucp_v1=1.0.0-KDQ1YTI5MDU4MzAwZGRmNGQxNTBiOWUyYTc4YjMxMjBhZGExMTA4MjYKGQjDvZj8lAMQ-8yIuQYY2hYgDDgGQPQHSAQaAmxmIiBmMTAxMGNjOTZhYjIwYTFhYTY0ZjJjMjI2N2Q2OGQ2OA; gfkadpd=2906,33638; csrf_session_id=e666edeaca83adf476fb6eb94852e8cd; biz_trace_id=8ab3afa5; ttwid=1%7CHHfPpEhOdMRc_iI6weyaJk-DeeWlxm8Dqa23du_Ly7Q%7C1730879771%7C5d3448b5c95e3306bdff94857b126759e35c6e5f021f1aaee304404f00a6f09f; bd_ticket_guard_client_data=eyJiZC10aWNrZXQtZ3VhcmQtdmVyc2lvbiI6MiwiYmQtdGlja2V0LWd1YXJkLWl0ZXJhdGlvbi12ZXJzaW9uIjoxLCJiZC10aWNrZXQtZ3VhcmQtcmVlLXB1YmxpYy1rZXkiOiJCSytMUm9sSkhCTERUK0p6dXdHd1FRdVZTNW1qSkxremZOb0FrcFpwVlgwZU9DRnh5dkgzbDNTcVg2cVlCTk9ZVTd3QU9zeFIxMk9OaFQyN2xia0JMNlU9IiwiYmQtdGlja2V0LWd1YXJkLXdlYi12ZXJzaW9uIjoyfQ%3D%3D; passport_fe_beating_status=true'))
